package com.swind.fast.modules.app.halali.customer.controller;

import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.swind.fast.common.api.vo.Result;
import com.swind.fast.common.system.query.QueryGenerator;
import com.swind.fast.common.util.oConvertUtils;
import com.swind.fast.common.constant.CommonConstant;
import com.swind.fast.modules.app.halali.customer.entity.Order;
import com.swind.fast.modules.app.halali.customer.entity.OrderItem;
import com.swind.fast.modules.app.halali.customer.service.IOrderService;

import com.swind.fast.modules.app.halali.customer.entity.RestaurantMeal;
import com.swind.fast.modules.app.halali.customer.service.IRestaurantMealService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import com.swind.fast.common.system.base.controller.SwindController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
// import com.swind.fast.common.aspect.annotation.AutoLog;

import com.swind.fast.modules.system.entity.SysAnnouncement;
import com.swind.fast.modules.system.entity.SysAnnouncementSend;
import com.swind.fast.modules.system.service.ISysAnnouncementSendService;
import com.swind.fast.modules.system.service.ISysAnnouncementService;
import java.util.Optional;
import java.text.DecimalFormat;

import org.apache.shiro.SecurityUtils;
import com.swind.fast.common.system.vo.HalaliLoginUser;
import com.swind.fast.common.system.vo.LoginUser;
import com.swind.fast.modules.app.halali.customer.vo.request.OrderVo;
import com.swind.fast.modules.app.halali.customer.vo.response.OrderDetailVo;

 /**
 * @Description: 餐厅
 * @Author: swind
 * @Date:   2022-01-20
 * @Version: V1.0
 */
// @Api(tags="用户端-订单")
@RestController
@RequestMapping("/app/customer/order")
@Slf4j
public class OrderController extends SwindController<Order, IOrderService> {
	@Autowired
	private IOrderService orderService;

	@Autowired
	private IRestaurantMealService restaurantMealService;

	// @Autowired
	// private ISysAnnouncementService sysAnnouncementService;

	/**
	 * 分页列表查询
	 *
	 * @param cart
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	// @AutoLog(value = "合同-分页列表查询")
	// @ApiOperation(value="订单-分页列表查询", notes="订单-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(Order cart,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		HalaliLoginUser sysUser = (HalaliLoginUser) SecurityUtils.getSubject().getPrincipal();
		// LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String userId = sysUser.getId();
		cart.setUserId(userId);
		QueryWrapper<Order> queryWrapper = QueryGenerator.initQueryWrapper(cart, req.getParameterMap());
		Page<Order> page = new Page<Order>(pageNo, pageSize);
		IPage<Order> pageList = orderService.page(page, queryWrapper);
		pageList = orderService.renderPage(pageList);
		return Result.OK(pageList);
	}

	// @ApiOperation(value="订单-用户商品分页查询", notes="订单-用户商品分页查询")
	// @GetMapping(value = "/items")
	// public Result<?> queryUserItemsPageList(Order cart,
	// 							   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
	// 							   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
	// 							   HttpServletRequest req) {
	// 	QueryWrapper<Order> queryWrapper = QueryGenerator.initQueryWrapper(cart, req.getParameterMap());
	// 	Page<Order> page = new Page<Order>(pageNo, pageSize);
	// 	IPage<Order> pageList = orderService.page(page, queryWrapper);
	// 	return Result.OK(pageList);
	// }

	/**
	 *   添加
	 *
	 * @param cart
	 * @return
	 */
	// @AutoLog(value = "账单-添加")
	// @ApiOperation(value="订单-创建", notes="订单-创建")
	// @PostMapping(value = "/add")
	// public Result<?> add(@RequestBody Order order) {
	// 	// spaceContractBillService.save(spaceContractBill);
	// 	return Result.OK("添加成功！", null);
	// }

	/**
	 *   添加
	 *
	 * @param cart
	 * @return
	 */
	// @AutoLog(value = "账单-添加")
	// @ApiOperation(value="订单-确认", notes="订单-确认")
	@PostMapping(value = "/confirm")
	public Result<?> confirm(@RequestBody OrderVo orderVo) {
		// spaceContractBillService.save(spaceContractBill);
		Order order = orderService.confirm(orderVo);
		return Result.OK("确认成功！", order);
	}

	/**
	 *   添加
	 *
	 * @param cart
	 * @return
	 */
	// @AutoLog(value = "账单-添加")
	// @ApiOperation(value="订单-取消", notes="订单-取消")
	// @PostMapping(value = "/cancel")
	// public Result<?> cancel(@RequestBody Order order) {
	// 	// spaceContractBillService.save(spaceContractBill);
	// 	return Result.OK("取消成功！", null);
	// }

	/**
	 *   添加
	 *
	 * @param cart
	 * @return
	 */
	// @AutoLog(value = "账单-添加")
	// @ApiOperation(value="订单-再来一单", notes="订单-再来一单")
	@PostMapping(value = "/reorder")
	public Result<?> reorder(@RequestBody Order order) {
		// spaceContractBillService.save(spaceContractBill);
		return Result.OK("确认成功！", null);
	}

	// @ApiOperation(value="订单-通过id查询", notes="账单-通过id查询")
	@GetMapping(value = "/detail")
	public Result<?> detail(@RequestParam(name="id", required=true) String id) {
		OrderDetailVo orderDetail = orderService.getDetailById(id);
		return Result.OK(orderDetail);
	}

}
